<template>
  <view class="backpack-container">
    <!-- 物品统计 -->
    <view class="backpack-stats">
      <text class="stats-text">共 {{ totalItems }} 件物品</text>
    </view>
    
    <!-- 分类标签 -->
    <view class="category-tabs">
      <view 
        class="tab-item" 
        :class="{ active: activeCategory === 'all' }" 
        @click="switchCategory('all')"
      >
        <text class="tab-icon">🎮</text>
        <text class="tab-text">全部</text>
      </view>
      <view 
        class="tab-item" 
        :class="{ active: activeCategory === 'weapon' }" 
        @click="switchCategory('weapon')"
      >
        <text class="tab-icon">⚔️</text>
        <text class="tab-text">武器</text>
      </view>
      <view 
        class="tab-item" 
        :class="{ active: activeCategory === 'skin' }" 
        @click="switchCategory('skin')"
      >
        <text class="tab-icon">🎨</text>
        <text class="tab-text">皮肤</text>
      </view>
      <view 
        class="tab-item" 
        :class="{ active: activeCategory === 'prop' }" 
        @click="switchCategory('prop')"
      >
        <text class="tab-icon">💎</text>
        <text class="tab-text">道具</text>
      </view>
      <view 
        class="tab-item" 
        :class="{ active: activeCategory === 'blindbox' }" 
        @click="switchCategory('blindbox')"
      >
        <text class="tab-icon">📦</text>
        <text class="tab-text">盲盒</text>
      </view>
      <view 
        class="tab-item" 
        :class="{ active: activeCategory === 'lottery' }" 
        @click="switchCategory('lottery')"
      >
        <text class="tab-icon">🎰</text>
        <text class="tab-text">抽奖</text>
      </view>
      <view 
        class="tab-item" 
        :class="{ active: activeCategory === 'pet' }" 
        @click="switchCategory('pet')"
      >
        <text class="tab-icon">🐾</text>
        <text class="tab-text">宠物</text>
      </view>
      <view 
        class="tab-item" 
        :class="{ active: activeCategory === 'vehicle' }" 
        @click="switchCategory('vehicle')"
      >
        <text class="tab-icon">🚗</text>
        <text class="tab-text">载具</text>
      </view>
    </view>
    
    <!-- 物品列表 -->
    <scroll-view class="items-scroll" scroll-y="true">
      <view class="items-grid">
        <view 
          class="item-card" 
          v-for="item in filteredItems" 
          :key="item.id"
          @click="showItemDetail(item)"
        >
          <view class="item-image">
            <text class="item-icon">{{ item.icon }}</text>
            <view class="item-quantity" v-if="item.quantity > 1">
              <text class="quantity-text">{{ item.quantity }}</text>
            </view>
          </view>
          <view class="item-info">
            <text class="item-name">{{ item.name }}</text>
            <text class="item-type">{{ item.typeText }}</text>
            <text class="item-desc">{{ item.description }}</text>
          </view>
          <view class="item-actions">
            <view class="action-btn use-btn" @click.stop="useItem(item)" v-if="item.canUse">
              <text class="btn-text">使用</text>
            </view>
            <view class="action-btn sell-btn" @click.stop="sellItem(item)" v-if="item.canSell">
              <text class="btn-text">出售</text>
            </view>
          </view>
        </view>
        
        <!-- 空状态 -->
        <view class="empty-backpack" v-if="filteredItems.length === 0">
          <text class="empty-icon">🎒</text>
          <text class="empty-text">背包空空如也</text>
          <text class="empty-desc">去商城购买一些物品吧</text>
          <view class="go-shop-btn" @click="goToShop">
            <text class="shop-btn-text">前往商城</text>
          </view>
        </view>
      </view>
    </scroll-view>
    
    <!-- 物品详情弹窗 -->
    <view class="item-modal" v-if="showModal" @click="hideItemDetail">
      <view class="modal-content" @click.stop>
        <view class="modal-header">
          <text class="modal-title">{{ selectedItem.name }}</text>
          <text class="modal-close" @click="hideItemDetail">✕</text>
        </view>
        
        <view class="modal-body">
          <view class="item-detail-image">
            <text class="detail-icon">{{ selectedItem.icon }}</text>
          </view>
          
          <view class="item-detail-info">
            <text class="detail-name">{{ selectedItem.name }}</text>
            <text class="detail-type">{{ selectedItem.typeText }}</text>
            <text class="detail-desc">{{ selectedItem.description }}</text>
            <text class="detail-quantity">数量: {{ selectedItem.quantity }}</text>
            <text class="detail-price">购买价格: {{ selectedItem.price }}积分</text>
          </view>
          
          <view class="modal-actions">
            <view class="modal-btn use-btn" @click="useItem(selectedItem)" v-if="selectedItem.canUse">
              <text class="modal-btn-text">使用</text>
            </view>
            <view class="modal-btn sell-btn" @click="sellItem(selectedItem)" v-if="selectedItem.canSell">
              <text class="modal-btn-text">出售</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import backpackManager from '@/utils/backpack-manager.js'

export default {
  data() {
    return {
      activeCategory: 'all',
      showModal: false,
      selectedItem: null,
      backpackItems: []
    }
  },
  
  computed: {
    // 过滤后的物品列表
    filteredItems() {
      if (this.activeCategory === 'all') {
        return this.backpackItems
      }
      return this.backpackItems.filter(item => item.type === this.activeCategory)
    },
    
    // 总物品数量
    totalItems() {
      return this.backpackItems.reduce((total, item) => total + item.quantity, 0)
    }
  },
  
  onLoad() {
    this.loadBackpackData()
  },
  
  onShow() {
    // 每次显示页面时重新加载数据，确保显示最新购买的物品
    this.loadBackpackData()
  },
  
  methods: {
    // 加载背包数据
    loadBackpackData() {
      try {
        this.backpackItems = backpackManager.getAllItems()
      } catch (error) {
        console.error('加载背包数据失败:', error)
        uni.showToast({
          title: '加载背包数据失败',
          icon: 'none'
        })
      }
    },
    
    
    // 切换分类
    switchCategory(category) {
      this.activeCategory = category
    },
    
    // 显示物品详情
    showItemDetail(item) {
      this.selectedItem = item
      this.showModal = true
    },
    
    // 隐藏物品详情
    hideItemDetail() {
      this.showModal = false
      this.selectedItem = null
    },
    
    // 使用物品
    useItem(item) {
      if (item.quantity <= 0) {
        uni.showToast({
          title: '物品数量不足',
          icon: 'none'
        })
        return
      }
      
      uni.showModal({
        title: '确认使用',
        content: `确定要使用 ${item.name} 吗？`,
        success: (res) => {
          if (res.confirm) {
            const success = backpackManager.useItem(item.id, 1)
            
            if (success) {
              // 重新加载背包数据
              this.loadBackpackData()
              
              uni.showToast({
                title: '使用成功',
                icon: 'success'
              })
              
              this.hideItemDetail()
            } else {
              uni.showToast({
                title: '使用失败',
                icon: 'none'
              })
            }
          }
        }
      })
    },
    
    // 出售物品
    sellItem(item) {
      if (item.quantity <= 0) {
        uni.showToast({
          title: '物品数量不足',
          icon: 'none'
        })
        return
      }
      
      const sellPrice = Math.floor(item.price * 0.5) // 出售价格为购买价格的50%
      
      uni.showModal({
        title: '确认出售',
        content: `确定要出售 ${item.name} 吗？\n获得积分: ${sellPrice}`,
        success: (res) => {
          if (res.confirm) {
            const result = backpackManager.sellItem(item.id, 1)
            
            if (result.success) {
              // 重新加载背包数据
              this.loadBackpackData()
              
              // 增加积分
              // 这里应该调用积分管理器增加积分
              // pointsManager.addPoints(result.points, `出售${result.itemName}`)
              
              uni.showToast({
                title: `出售成功，获得${result.points}积分`,
                icon: 'success'
              })
              
              this.hideItemDetail()
            } else {
              uni.showToast({
                title: '出售失败',
                icon: 'none'
              })
            }
          }
        }
      })
    },
    
    // 前往商城
    goToShop() {
      uni.navigateTo({
        url: '/pages/shop/shop'
      })
    },
    
  }
}
</script>

<style scoped>
.backpack-container {
  min-height: 100vh;
  background: #f5f5f5;
}

/* 物品统计 */
.backpack-stats {
  padding: 20rpx;
  background: white;
  border-bottom: 1rpx solid #e0e0e0;
}

.stats-text {
  font-size: 24rpx;
  color: #636e72;
}

/* 分类标签 */
.category-tabs {
  display: flex;
  background: white;
  padding: 0 20rpx;
  border-bottom: 1rpx solid #e0e0e0;
  overflow-x: auto;
}

.tab-item {
  flex-shrink: 0;
  text-align: center;
  padding: 20rpx 16rpx;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4rpx;
  min-width: 80rpx;
}

.tab-item.active {
  color: #667eea;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40rpx;
  height: 4rpx;
  background: #667eea;
  border-radius: 2rpx;
}

.tab-icon {
  font-size: 24rpx;
  color: inherit;
}

.tab-text {
  font-size: 20rpx;
  color: inherit;
}

/* 物品列表 */
.items-scroll {
  height: calc(100vh - 300rpx);
  padding: 20rpx;
}

.items-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
}

.item-card {
  background: white;
  border-radius: 16rpx;
  padding: 20rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.item-card:active {
  transform: scale(0.95);
}

.item-image {
  position: relative;
  text-align: center;
  margin-bottom: 16rpx;
}

.item-icon {
  font-size: 48rpx;
  display: block;
}

.item-quantity {
  position: absolute;
  top: -8rpx;
  right: -8rpx;
  background: #ff6b6b;
  color: white;
  border-radius: 50%;
  width: 32rpx;
  height: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.quantity-text {
  font-size: 18rpx;
  font-weight: bold;
}

.item-info {
  margin-bottom: 16rpx;
}

.item-name {
  font-size: 24rpx;
  font-weight: bold;
  color: #2d3436;
  display: block;
  margin-bottom: 4rpx;
}

.item-type {
  font-size: 20rpx;
  color: #667eea;
  display: block;
  margin-bottom: 8rpx;
}

.item-desc {
  font-size: 18rpx;
  color: #636e72;
  line-height: 1.4;
  display: block;
}

.item-actions {
  display: flex;
  gap: 8rpx;
}

.action-btn {
  flex: 1;
  text-align: center;
  padding: 12rpx 0;
  border-radius: 8rpx;
  font-size: 20rpx;
}

.use-btn {
  background: #4CAF50;
  color: white;
}

.sell-btn {
  background: #ff9800;
  color: white;
}

.btn-text {
  font-size: 20rpx;
  color: inherit;
}

/* 空状态 */
.empty-backpack {
  grid-column: 1 / -1;
  text-align: center;
  padding: 80rpx 20rpx;
  color: #999;
}

.empty-icon {
  font-size: 120rpx;
  display: block;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 32rpx;
  font-weight: bold;
  display: block;
  margin-bottom: 12rpx;
}

.empty-desc {
  font-size: 24rpx;
  display: block;
  margin-bottom: 30rpx;
}

.go-shop-btn {
  display: inline-block;
  background: #667eea;
  color: white;
  padding: 20rpx 40rpx;
  border-radius: 12rpx;
}

.shop-btn-text {
  font-size: 24rpx;
  color: white;
}

/* 物品详情弹窗 */
.item-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 20rpx;
  width: 90%;
  max-width: 600rpx;
  max-height: 80vh;
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #e0e0e0;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #2d3436;
}

.modal-close {
  font-size: 32rpx;
  color: #999;
  cursor: pointer;
}

.modal-body {
  padding: 30rpx;
}

.item-detail-image {
  text-align: center;
  margin-bottom: 30rpx;
}

.detail-icon {
  font-size: 80rpx;
  display: block;
}

.item-detail-info {
  margin-bottom: 30rpx;
}

.detail-name {
  font-size: 28rpx;
  font-weight: bold;
  color: #2d3436;
  display: block;
  margin-bottom: 8rpx;
}

.detail-type {
  font-size: 22rpx;
  color: #667eea;
  display: block;
  margin-bottom: 12rpx;
}

.detail-desc {
  font-size: 20rpx;
  color: #636e72;
  line-height: 1.4;
  display: block;
  margin-bottom: 16rpx;
}

.detail-quantity,
.detail-price {
  font-size: 20rpx;
  color: #999;
  display: block;
  margin-bottom: 8rpx;
}

.modal-actions {
  display: flex;
  gap: 16rpx;
}

.modal-btn {
  flex: 1;
  text-align: center;
  padding: 20rpx 0;
  border-radius: 12rpx;
  font-size: 24rpx;
}

.modal-btn-text {
  font-size: 24rpx;
  color: white;
}
</style>
